//
//  UIButton+BaseAttributes.m
//  MonitoringNetwork
//
//  Created by admin on 2017/12/5.
//  Copyright © 2017年 wtmcxlm. All rights reserved.
//

#import "UIButton+BaseAttributes.h"

@implementation UIButton (BaseAttributes)



+ (UIButton *)buttonWithTitle:(NSString *)title
         normalTitleColor:(UIColor *)normalTitleColor
                 selectTitleColor:(UIColor *)selectTitleColor
                   titleFont:(UIFont *)titlefont{
    UIButton *button = [[UIButton alloc] init];
    button.titleLabel.font = titlefont;
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:normalTitleColor forState:UIControlStateNormal];
    [button setTitleColor:selectTitleColor forState:UIControlStateSelected];
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    return button;
}



+ (UIButton *)buttonWithTitle:(NSString *)title
             normalTitleColor:(UIColor *)normalTitleColor
             selectTitleColor:(UIColor *)selectTitleColor
              normalImage:(UIImage *)normalImage
              selectImage:(UIImage *)selectImage
                   titleFont:(UIFont *)titlefont{
    UIButton *button = [[UIButton alloc] init];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:normalTitleColor forState:UIControlStateNormal];
    [button setTitleColor:selectTitleColor forState:UIControlStateSelected];
    [button setImage:normalImage forState:UIControlStateNormal];
    [button setImage:selectImage forState:UIControlStateSelected];
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    button.titleLabel.font = titlefont;
    return button;
}

+ (UIButton *)buttonWithTitle:(NSString *)title
                    titleFont:(UIFont *)font
             normalTitleColor:(UIColor *)normalTitleColor
           selectedTitleColor:(UIColor *)selectTitleColor
         normalBackgoundImage:(UIImage *)normalBackgoundImage
       selectedBackgoundImage:(UIImage *)selectedBackgoundImage
{
    UIButton *button = [[UIButton alloc]init];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:normalTitleColor forState:UIControlStateNormal];
    [button setTitleColor:selectTitleColor forState:UIControlStateSelected];
    [button setBackgroundImage:normalBackgoundImage forState:UIControlStateNormal];
    [button setBackgroundImage:selectedBackgoundImage forState:UIControlStateSelected];
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    button.titleLabel.font = font;
    return button;
}
    
- (void)buttonWithCornerRadius:(CGFloat)cornerRadius
                    borderWidth:(CGFloat)borderWidth
                    borderColor:(UIColor*)borderColor
{
    self.clipsToBounds = YES;
    self.layer.cornerRadius = cornerRadius;
    self.layer.borderWidth = borderWidth;
    self.layer.borderColor = borderColor.CGColor;
    
}

/// 设置不同状态的背景图片
/// @param backgroundColor 背景图片
/// @param state 状态
- (void)setBackgroundColor:(UIColor *)backgroundColor
                  forState:(UIControlState)state{
    [self setBackgroundImage:[UIButton imageWithColor:backgroundColor] forState:state];
}

+ (UIImage *)imageWithColor:(UIColor *)color{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end
